package com.jy.adscm.operation.mscyb.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.operation.mscyb.bean.*;
import com.jy.adscm.operation.mscyb.mapper.mysql.JqbzPartsassemblyMapper;
import com.jy.adscm.operation.mscyb.mapper.mysql.JqbzResultMapper;
import com.jy.adscm.operation.mscyb.mapper.mysql.JqbzSettingOutCheckMapper;
import com.jy.adscm.operation.mscyb.mapper.mysql.JqbzSettingsMapper;
import com.jy.adscm.operation.mscyb.service.JqbzResultService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyl
 * @since 2019-05-25
 */
@Service
public class JqbzResultServiceImpl extends ServiceImpl<JqbzResultMapper, JqbzResult> implements JqbzResultService {
    @Autowired
    JyUserMapper  jyUserMapper;
    @Autowired
    JqbzPartsassemblyMapper jqbzPartsassemblyMapper;
    @Autowired
    JqbzSettingOutCheckMapper jqbzSettingOutCheckMapper;
    @Autowired
    JqbzResultMapper jqbzResultMapper;
    @Autowired
    JqbzSettingsMapper jqbzSettingsMapper;


    @Override
    public List<JqbzResultContainer> selectByPid(String pid) {
        List<JqbzResultContainer> jqbzResultContainers =new ArrayList<>();
        Wrapper<JqbzResult> jqbzResultWrapper =new EntityWrapper<>();
        jqbzResultWrapper.eq("pid",pid);
        jqbzResultWrapper.orderBy("idx",false);
        jqbzResultWrapper.last("LIMIT 1");
        List<JqbzResult> jqbzResults = super.selectList(jqbzResultWrapper);
         Integer idx=1;
         if (jqbzResults!=null && jqbzResults.size()>0){
             JqbzResult jqbzResult = jqbzResults.get(0);
            idx= jqbzResult.getIdx();
         }
        for (int i = 1; i <=idx; i++) {
            Wrapper<JqbzResult> wrapper =new EntityWrapper<>();
            wrapper.eq("pid",pid);
            wrapper.eq("idx",i);
            wrapper.orderBy("rank",false);
            List<JqbzResult> jqbzResults1 = super.selectList(wrapper);

            if (jqbzResults1!=null &&jqbzResults1.size()>0){

                for (JqbzResult jqbzResult : jqbzResults1) {
                    if (AdscmBaseUtils.getInstance().isNotEmpty(jqbzResult.getCreator(),null)){
                        JyUser jyUser = jyUserMapper.selectById(jqbzResult.getCreator());
                        jqbzResult.setCreatorName(jyUser.getName());
                    }
                }
                JqbzResult jqbzResult = jqbzResults1.get(0);
                JqbzResultContainer  jqbzResultContainer=new JqbzResultContainer();
                JqbzPartsassembly jqbzPartsassembly = jqbzPartsassemblyMapper.selectById(pid);
                JqbzSettings jqbzSettings = jqbzSettingsMapper.selectById(jqbzPartsassembly.getResourcename());
                jqbzResultContainer.setName(jqbzSettings.getName()+"检测记录");
                jqbzResultContainer.setCreator(jqbzResult.getCreatorName());
                jqbzResultContainer.setCreateTime(jqbzResult.getCreateTime());
                jqbzResultContainer.setJqbzResults(jqbzResults1);
                jqbzResultContainers.add(jqbzResultContainer);

            }

        }
        return jqbzResultContainers;
    }

    @Override
    public boolean updateJqbzResult(List<JqbzResult> jqbzResults , Token token) {
        for (JqbzResult jqbzResult : jqbzResults) {
            if ("检验结果".equals(  jqbzResult.getName())){
                JqbzPartsassembly jqbzPartsassembly = jqbzPartsassemblyMapper.selectById(jqbzResult.getPid());
                if ("1".equals(jqbzResult.getValue())){
                    jqbzPartsassembly.setState(Integer.valueOf("1"));
                    jqbzPartsassembly.setIsreviewtask("0");
                    jqbzPartsassemblyMapper.updateById(jqbzPartsassembly);
                }
                if ("0".equals(jqbzResult.getValue())){
                    jqbzPartsassembly.setState(Integer.valueOf("0"));
                    jqbzPartsassemblyMapper.updateById(jqbzPartsassembly);
                }
            }
            jqbzResult.setCreator(token.getUser().getId().toString());
            jqbzResult.setCreateTime(new Date());
            boolean b = super.updateById(jqbzResult);
        }
        return true;
    }

    @Override
    public boolean insertJqbzResult(JqbzResult jqbzResult) {
        JqbzPartsassembly jqbzPartsassembly = jqbzPartsassemblyMapper.selectById(jqbzResult.getPid());
          Wrapper<JqbzSettingOutCheck> wrapper =new EntityWrapper<>();
          wrapper.eq("pid",jqbzPartsassembly.getResourcename());
        wrapper.eq("isEnable",1);
        List<JqbzSettingOutCheck> jqbzSettingOutChecks = jqbzSettingOutCheckMapper.selectList(wrapper);
        //获取最大的编号
        Wrapper<JqbzResult> jqbzResultWrapper =new EntityWrapper<>();
        jqbzResultWrapper.eq("pid",jqbzResult.getPid());
        jqbzResultWrapper.orderBy("idx",false);
        jqbzResultWrapper.last("LIMIT 1");
        List<JqbzResult> jqbzResults = super.selectList(jqbzResultWrapper);
        Integer idx=1;
        if (jqbzResults!=null && jqbzResults.size()>0){
            JqbzResult jqbzResult1 = jqbzResults.get(0);
            idx=jqbzResult1.getIdx()+1;
        }
        for (JqbzSettingOutCheck jqbzSettingOutCheck : jqbzSettingOutChecks) {
            JqbzResult jqbzResult1=new JqbzResult();
            jqbzResult1.setIdx(idx);
            jqbzResult1.setPid(jqbzResult.getPid());
            jqbzResult1.setCid(jqbzSettingOutCheck.getId());
            jqbzResult1.setName(jqbzSettingOutCheck.getName());
            jqbzResult1.setReference(jqbzSettingOutCheck.getReference());
            jqbzResult1.setType(jqbzSettingOutCheck.getType());
            jqbzResult1.setUnit(jqbzSettingOutCheck.getUnit());
            jqbzResult1.setRemark(jqbzSettingOutCheck.getRemark());
            jqbzResult1.setCreator(jqbzResult.getCreator());
            jqbzResult1.setCreateTime(new Date());
            jqbzResult1.setRank(jqbzSettingOutCheck.getRank());
            jqbzResultMapper.insert(jqbzResult1);
        }
        return true;
    }
}
